<?php
class PaymentDuesController extends AppController {
    var $name = 'PaymentDues';

    function index() {
        $this->PaymentDue->recursive = 0;
/*		   $permit  = $this->Session->read('permit_build');  
		if ( $permit!= null ){
            $data = null;
            $new  = null;
            foreach ($permit as $key => $value) {
                $this->paginate = array(
                             'conditions' => array( 'PaymentDue.building_id' => $value )
                         );  
                $data[$key]  = $this->paginate();
            }   
            foreach ( $data as $key1 => $value1)
            {   
                $new[$key1] = $value1[0];
            }   
            $this->set('paymentDues', $new);
        }else{
              $this->Session->setFlash(__('You have nothing assigned', true));
              $this->redirect(array('controller' => 'users', 'action' => 'welcome'));
        }  
 */
        $this->set('paymentDues', $this->paginate());
  	}

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid payment due', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('paymentDue', $this->PaymentDue->read(null, $id));
    }

    function add() {
        if (!empty($this->data)) {
            $this->PaymentDue->create();
            if ($this->PaymentDue->save($this->data)) {
                $this->Session->setFlash(__('The payment due has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The payment due could not be saved. Please, try again.', true));
            }
        }
        $buildings = $this->PaymentDue->Building->find('list');
        $interestSchemes = array('empty' => 'Select',$this->PaymentDue->InterestScheme->find('list',
                array('conditions' => array('InterestScheme.lookup_type' => 'INTEREST_SCHEME_TYPE'))));
        $this->set(compact('buildings','interestSchemes'));
    }

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid payment due', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->PaymentDue->save($this->data)) {
                $this->Session->setFlash(__('The payment due has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The payment due could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->PaymentDue->read(null, $id);
        }
        $buildings = $this->PaymentDue->Building->find('list');
        $interestSchemes = array('empty' => 'Select',$this->PaymentDue->InterestScheme->find('list',
                array('conditions' => array('InterestScheme.lookup_type' => 'INTEREST_SCHEME_TYPE'))));
        $this->set(compact('buildings','interestSchemes'));
    }
}
?>
